﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook;

namespace lydoe._1110i
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string host = Request.ServerVariables["HTTP_HOST"];
            if (!IsPostBack)
            {
                if (Request.QueryString["code"] != null)
                {
                    //nothing
                }
                else
                {
                    string spath = @"https://graph.facebook.com/oauth/authorize?client_id=" +
                                   "206388329486388" + "&redirect_uri=http://" + host +
                                   @"/1110i/default.aspx&scope=publish_stream";
                    Response.Redirect(spath);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile != null)
            {
                HttpPostedFile myFile = FileUpload1.PostedFile;
                int nFileLen = myFile.ContentLength;
                if (nFileLen > 0)
                {
                    //byte[] myData = new byte[nFileLen];
                    //myFile.InputStream.Read(myData, 0, nFileLen);
                    string strFilename = Path.GetFileName(myFile.FileName);
                    string sf = strFilename.ToUpper();
                    if (sf.EndsWith(".JPG") || sf.EndsWith(".PNG") || sf.EndsWith(".BMP") || sf.EndsWith(".GIF"))
                    {
                        string sFile = Server.MapPath("/1110i/image/") + strFilename;
                        FileUpload1.SaveAs(sFile);
                        post(sFile);
                    }
                }
                else
                {
                    post("");
                }
            }
            else
            {
                post("");
            }
        }

        private void post(string sFile)
        {
            string host = Request.ServerVariables["HTTP_HOST"];
            var cl = new FacebookOAuthClient();
            cl.AppId = "206388329486388";
            cl.AppSecret = "779acb9d5735b1456704c4dd0541e7f7";
            cl.RedirectUri = new Uri("http://" + host + "/1110i/default.aspx");
            var code = (IDictionary<string, object>)cl.ExchangeCodeForAccessToken(Request.QueryString["code"]);
            var fb = new Facebook.FacebookClient((string)code["access_token"]);
            //var me = (IDictionary<string, object>)fb.Get("/me");
            var ex = new Dictionary<string, object>();

            if (sFile != "")
            {
                var facebookMedia = new FacebookMediaObject
                    {
                        ContentType = "image/jpg",
                        FileName = sFile
                    };

                var bytes = File.ReadAllBytes(sFile);
                facebookMedia.SetValue(bytes);
                ex["source"] = facebookMedia;
                ex["message"] = TextBox1.Text;
                var result = fb.Post("me/photos", ex);
            }
            else
            {
                ex["message"] = TextBox1.Text;
                //ex["caption"] = "This is caption!";
                //ex["description"] = "This is description!";
                //ex["name"] = "This is name!";
                //ex["picture"] = "[your image URL]";
                //ex["link"] = "[your link URL]";
                var result = fb.Post("me/feed", ex);
            }
        }
    }
}
